<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件

function base_result($data, $success = true, $error_code = '', $error_msg = '', $page_size = 0, $data_count = 0, $page_index = 0)
{
    $result =   array('success' => $success, 'data' => $data, 'error_code' => $error_code, 'error_msg' => $error_msg);
    if($page_size!=0){
        $page_count = floor($data_count / $page_size);
        if ($data_count % $page_size != 0) {
            $page_count++;
        }
        $result["page"]=array('page_size'=>$page_size,'data_count'=>$data_count,
            'page_index'=>$page_index,'page_count'=>$page_count,'query_time'=>date('Y-m-d H:i:s'));
    }
    return json($result);
}

function isSsl()
{
    if (isset($_SERVER['HTTPS']) && ('1' == $_SERVER['HTTPS'] || 'on' == strtolower($_SERVER['HTTPS']))) {
        return true;
    } elseif (isset($_SERVER['SERVER_PORT']) && ('443' == $_SERVER['SERVER_PORT'])) {
        return true;
    }
    return false;
}

function is_wechat_browser()
{
    $user_agent = strtolower($_SERVER["HTTP_USER_AGENT"]);

    if (strpos($user_agent, "micromessenger") === false) {
        return false;
    } else {
        return true;
    }
}

/** 生成随机数
 * @param int $length
 * @return string
 */
function generate_code($length = 4)
{
    $min = pow(10, ($length - 1));
    $max = pow(10, $length) - 1;
    return (string)rand($min, $max);
}

/**
 * 验证手机号是否正确
 * @author honfei
 * @param number $mobile
 */
function is_mobile($mobile)
{
    if (!is_numeric($mobile)) {
        return false;
    }
    return preg_match('#^13[\d]{9}$|^14[5,7]{1}\d{8}$|^15[^4]{1}\d{8}$|^17[0,6,7,8]{1}\d{8}$|^18[\d]{9}$#', $mobile) ? true : false;
}


function array2object($array)
{
    if (is_array($array)) {
        $obj = new StdClass();
        foreach ($array as $key => $val) {
            $obj->$key = $val;
        }
    } else {
        $obj = $array;
    }
    return $obj;
}

function object2array($object)
{
    if (is_object($object)) {
        foreach ($object as $key => $value) {
            $array[$key] = $value;
        }
    } else {
        $array = $object;
    }
    return $array;
}

function date_startend_time($t)
{
    $array['start'] = mktime(0,0,0,date("m",$t),date("d",$t),date("Y",$t));
    $array['end'] = mktime(23,59,59,date("m",$t),date("d",$t),date("Y",$t));
    return $array;
}

function getTokenInfo()
{
    // \think\Request::instance()->bind('worker_user', array('user_id' => 30));
    $request = \think\Request::instance();
    $result = $request->worker_user;
    // $result = $result ? $result->user_id : $result;
    // $result = Request::instance()->bind('worker_user');
    return $result;
}

//https请求（支持GET和POST）
function https_request($url, $data = null)
{
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
    if (!empty($data)){
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
    }
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    $output = curl_exec($curl);
    curl_close($curl);
    return $output;
}

// 下载url图片
function put_file_from_url_content($url, $saveName, $path) {
    // 设置运行时间为无限制
    set_time_limit ( 0 );
    
    $url = trim ( $url );
    $ch=curl_init();
    $timeout=5;
    curl_setopt($ch,CURLOPT_URL,$url);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
    $file=curl_exec($ch);
    curl_close($ch);
    // 将文件写入获得的数据
    $path1 = APP_ROOT_PATH . $path;
    $filename = $path1 . $saveName;
    if (!file_exists($path1)) {
    //检查是否有该文件夹，如果没有就创建，并给予最高权限
        mkdir($path1, 0777);
    }
    $write = @fopen ( $filename, "w" );
    if ($write == false) {
        return false;
    }
    if (fwrite ( $write, $file ) == false) {
        return false;
    }
    if (fclose ( $write ) == false) {
        return false;
    }
    return array('file_name'=>$saveName,'path'=>$path,'save_path'=>$filename);
}

